using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ApplyDAL
    {
        public static int Save(ApplyInfo info)
        {
            string sql = string.Format("insert into [apply]([UserID],[AdminID],[NickName],[HeadImg],[Name],[Mobile],[City],[ComName],[CreateDate]) values(@UserID,@AdminID,@NickName,@HeadImg,@Name,@Mobile,@City,@ComName,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@AdminID", info.Adminid),
                new SqlParameter("@NickName", info.NickName),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@City", info.City),
                new SqlParameter("@ComName", info.ComName),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(ApplyInfo info)
        {
            string sql = string.Format("update [apply] set [NickName]=@NickName,[HeadImg]=@HeadImg,[Name]=@Name,[Mobile]=@Mobile,[City]=@City,[ComName]=@ComName where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@NickName", info.NickName),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@City", info.City),
                new SqlParameter("@ComName", info.ComName),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }

        public static List<ApplyInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[apply]");
            return ListModel(dt);
        }
        public static ApplyInfo FindOneByID(string id)
        {
             ApplyInfo info=new ApplyInfo();
             info.Adminid = DataConvert.ToInt(CommonDAL.ExecuteScalar("select [Adminid] from [apply] where id="+id));
             return info;
        }
        public static List<ApplyInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (B.MyAdmin.Role==2)
            {
                filter.AppendFormat(" and [apply].[AdminID] ={0}",B.MyAdmin.ID);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [apply].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [apply].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter.ToString(), "[apply]", out count);
            return ListModel(dt);
        }
        public static List<ApplyInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [apply]"));
            return ListModel(dt);
        }
        private static ApplyInfo Model(DataRow dr, DataColumnCollection col)
        {
            ApplyInfo info = new ApplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.NickName = DataConvert.ToString(dr["NickName"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.ComName = DataConvert.ToString(dr["ComName"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.Adminid = DataConvert.ToInt(dr["AdminID"]);
                info.AdminName = DataConvert.ToString(CommonDAL.ExecuteScalar("select [RealName] from [admin] where id=" + info.Adminid));
            }
            return info;
        }
        private static ApplyInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0], dt.Columns);
            }
            else
            {
                return new ApplyInfo();
            }
        }
        private static List<ApplyInfo> ListModel(DataTable dt)
        {
            List<ApplyInfo> list = new List<ApplyInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row, col));
            }
            return list;
        }
    }
}

